integration with Title and Revision (work in progress)
[lhc/web/wiklou.git] / includes / Article.php
1 <?php
2 /**
3 * File for articles
4 * @file
5 */
6
7 /**
8 * Class for viewing MediaWiki article and history.
9 *
10 * This maintains WikiPage functions for backwards compatibility.
11 *
12 * @TODO: move and rewrite code to an Action class
13 *
14 * See design.txt for an overview.
15 * Note: edit user interface and cache support functions have been
16 * moved to separate EditPage and HTMLFileCache classes.
17 *
18 * @internal documentation reviewed 15 Mar 2010
19 */
20 class Article extends Page {
21 /**@{{
22 * @private
23 */
24
25 /**
26 * @var IContextSource
27 */
28 protected $mContext;
29
30 /**
31 * @var WikiPage
32 */
33 protected $mPage;
34
35 /**
36 * @var ParserOptions: ParserOptions object for $wgUser articles
37 */
38 public $mParserOptions;
39
40 var $mContent; // !< #FIXME: use Content object!
41 var $mContentLoaded = false; // !<
42 var $mOldId; // !<
43
44 /**
45 * @var Title
46 */
47 var $mRedirectedFrom = null;
48
49 /**
50 * @var mixed: boolean false or URL string
51 */
52 var $mRedirectUrl = false; // !<
53 var $mRevIdFetched = 0; // !<
54
55 /**
56 * @var Revision
57 */
58 var $mRevision = null;
59
60 /**
61 * @var ParserOutput
62 */
63 var $mParserOutput;
64
65 /**@}}*/
66
67 /**
68 * Constructor and clear the article
69 * @param $title Title Reference to a Title object.
70 * @param $oldId Integer revision ID, null to fetch from request, zero for current
71 */
72 public function __construct( Title $title, $oldId = null ) {
73 $this->mOldId = $oldId;
74 $this->mPage = $this->newPage( $title );
75 }
76
77 /**
78 * @param $title Title
79 * @return WikiPage
80 */
81 protected function newPage( Title $title ) {
82 return new WikiPage( $title );
83 }
84
85 /**
86 * Constructor from a page id
87 * @param $id Int article ID to load
88 * @return Article|null
89 */
90 public static function newFromID( $id ) {
91 $t = Title::newFromID( $id );
92 # @todo FIXME: Doesn't inherit right
93 return $t == null ? null : new self( $t );
94 # return $t == null ? null : new static( $t ); // PHP 5.3
95 }
96
97 /**
98 * Create an Article object of the appropriate class for the given page.
99 *
100 * @param $title Title
101 * @param $context IContextSource
102 * @return Article object
103 */
104 public static function newFromTitle( $title, IContextSource $context ) {
105 if ( NS_MEDIA == $title->getNamespace() ) {
106 // FIXME: where should this go?
107 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
108 }
109
110 $page = null;
111 wfRunHooks( 'ArticleFromTitle', array( &$title, &$page ) );
112 if ( !$page ) {
113 switch( $title->getNamespace() ) {
114 case NS_FILE:
115 $page = new ImagePage( $title );
116 break;
117 case NS_CATEGORY:
118 $page = new CategoryPage( $title );
119 break;
120 default:
121 $page = new Article( $title );
122 }
123 }
124 $page->setContext( $context );
125
126 return $page;
127 }
128
129 /**
130 * Create an Article object of the appropriate class for the given page.
131 *
132 * @param $page WikiPage
133 * @param $context IContextSource
134 * @return Article object
135 */
136 public static function newFromWikiPage( WikiPage $page, IContextSource $context ) {
137 $article = self::newFromTitle( $page->getTitle(), $context );
138 $article->mPage = $page; // override to keep process cached vars
139 return $article;
140 }
141
142 /**
143 * Tell the page view functions that this view was redirected
144 * from another page on the wiki.
145 * @param $from Title object.
146 */
147 public function setRedirectedFrom( Title $from ) {
148 $this->mRedirectedFrom = $from;
149 }
150
151 /**
152 * Get the title object of the article
153 *
154 * @return Title object of this page
155 */
156 public function getTitle() {
157 return $this->mPage->getTitle();
158 }
159
160 /**
161 * Get the WikiPage object of this instance
162 *
163 * @since 1.19
164 * @return WikiPage
165 */
166 public function getPage() {
167 return $this->mPage;
168 }
169
170 /**
171 * Clear the object
172 */
173 public function clear() {
174 $this->mContentLoaded = false;
175
176 $this->mRedirectedFrom = null; # Title object if set
177 $this->mRevIdFetched = 0;
178 $this->mRedirectUrl = false;
179
180 $this->mPage->clear();
181 }
182
183 /**
184 * Note that getContent/loadContent do not follow redirects anymore.
185 * If you need to fetch redirectable content easily, try
186 * the shortcut in WikiPage::getRedirectTarget()
187 *
188 * This function has side effects! Do not use this function if you
189 * only want the real revision text if any.
190 *
191 * @return Return the text of this revision
192 */
193 public function getContent() {
194 global $wgUser;
195
196 wfProfileIn( __METHOD__ );
197
198 if ( $this->mPage->getID() === 0 ) {
199 # If this is a MediaWiki:x message, then load the messages
200 # and return the message value for x.
201 if ( $this->getTitle()->getNamespace() == NS_MEDIAWIKI ) {
202 $text = $this->getTitle()->getDefaultMessageText();
203 if ( $text === false ) {
204 $text = '';
205 }
206 } else {
207 $text = wfMsgExt( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon', 'parsemag' );
208 }
209 wfProfileOut( __METHOD__ );
210
211 return $text;
212 } else {
213 $this->fetchContent();
214 wfProfileOut( __METHOD__ );
215
216 return $this->mContent;
217 }
218 }
219
220 /**
221 * @return int The oldid of the article that is to be shown, 0 for the
222 * current revision
223 */
224 public function getOldID() {
225 if ( is_null( $this->mOldId ) ) {
226 $this->mOldId = $this->getOldIDFromRequest();
227 }
228
229 return $this->mOldId;
230 }
231
232 /**
233 * Sets $this->mRedirectUrl to a correct URL if the query parameters are incorrect
234 *
235 * @return int The old id for the request
236 */
237 public function getOldIDFromRequest() {
238 global $wgRequest;
239
240 $this->mRedirectUrl = false;
241
242 $oldid = $wgRequest->getIntOrNull( 'oldid' );
243
244 if ( $oldid === null ) {
245 return 0;
246 }
247
248 if ( $oldid !== 0 ) {
249 # Load the given revision and check whether the page is another one.
250 # In that case, update this instance to reflect the change.
251 $this->mRevision = Revision::newFromId( $oldid );
252 if ( $this->mRevision !== null ) {
253 // Revision title doesn't match the page title given?
254 if ( $this->mPage->getID() != $this->mRevision->getPage() ) {
255 $function = array( get_class( $this->mPage ), 'newFromID' );
256 $this->mPage = call_user_func( $function, $this->mRevision->getPage() );
257 }
258 }
259 }
260
261 if ( $wgRequest->getVal( 'direction' ) == 'next' ) {
262 $nextid = $this->getTitle()->getNextRevisionID( $oldid );
263 if ( $nextid ) {
264 $oldid = $nextid;
265 $this->mRevision = null;
266 } else {
267 $this->mRedirectUrl = $this->getTitle()->getFullURL( 'redirect=no' );
268 }
269 } elseif ( $wgRequest->getVal( 'direction' ) == 'prev' ) {
270 $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
271 if ( $previd ) {
272 $oldid = $previd;
273 $this->mRevision = null;
274 }
275 }
276
277 return $oldid;
278 }
279
280 /**
281 * Load the revision (including text) into this object
282 *
283 * @deprecated in 1.19; use fetchContent()
284 */
285 function loadContent() {
286 wfDeprecated( __METHOD__, '1.19' );
287 $this->fetchContent();
288 }
289
290 /**
291 * Get text of an article from database
292 * Does *NOT* follow redirects.
293 *
294 * @return mixed string containing article contents, or false if null
295 */
296 function fetchContent() {
297 if ( $this->mContentLoaded ) {
298 return $this->mContent;
299 }
300
301 wfProfileIn( __METHOD__ );
302
303 $this->mContentLoaded = true;
304
305 $oldid = $this->getOldID();
306
307 # Pre-fill content with error message so that if something
308 # fails we'll have something telling us what we intended.
309 $t = $this->getTitle()->getPrefixedText();
310 $d = $oldid ? wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid ) : '';
311 $this->mContent = wfMsgNoTrans( 'missing-article', $t, $d ) ;
312
313 if ( $oldid ) {
314 # $this->mRevision might already be fetched by getOldIDFromRequest()
315 if ( !$this->mRevision ) {
316 $this->mRevision = Revision::newFromId( $oldid );
317 if ( !$this->mRevision ) {
318 wfDebug( __METHOD__ . " failed to retrieve specified revision, id $oldid\n" );
319 wfProfileOut( __METHOD__ );
320 return false;
321 }
322 }
323 } else {
324 if ( !$this->mPage->getLatest() ) {
325 wfDebug( __METHOD__ . " failed to find page data for title " . $this->getTitle()->getPrefixedText() . "\n" );
326 wfProfileOut( __METHOD__ );
327 return false;
328 }
329
330 $this->mRevision = $this->mPage->getRevision();
331 if ( !$this->mRevision ) {
332 wfDebug( __METHOD__ . " failed to retrieve current page, rev_id " . $this->mPage->getLatest() . "\n" );
333 wfProfileOut( __METHOD__ );
334 return false;
335 }
336 }
337
338 // @todo FIXME: Horrible, horrible! This content-loading interface just plain sucks.
339 // We should instead work with the Revision object when we need it...
340 $this->mContent = $this->mRevision->getText( Revision::FOR_THIS_USER ); // Loads if user is allowed
341 $this->mRevIdFetched = $this->mRevision->getId();
342
343 wfRunHooks( 'ArticleAfterFetchContent', array( &$this, &$this->mContent ) );
344
345 wfProfileOut( __METHOD__ );
346
347 return $this->mContent;
348 }
349
350 /**
351 * No-op
352 * @deprecated since 1.18
353 */
354 public function forUpdate() {
355 wfDeprecated( __METHOD__, '1.18' );
356 }
357
358 /**
359 * Returns true if the currently-referenced revision is the current edit
360 * to this page (and it exists).
361 * @return bool
362 */
363 public function isCurrent() {
364 # If no oldid, this is the current version.
365 if ( $this->getOldID() == 0 ) {
366 return true;
367 }
368
369 return $this->mPage->exists() && $this->mRevision && $this->mRevision->isCurrent();
370 }
371
372 /**
373 * Get the fetched Revision object depending on request parameters or null
374 * on failure.
375 *
376 * @since 1.19
377 * @return Revision|null
378 */
379 public function getRevisionFetched() {
380 $this->fetchContent();
381
382 return $this->mRevision;
383 }
384
385 /**
386 * Use this to fetch the rev ID used on page views
387 *
388 * @return int revision ID of last article revision
389 */
390 public function getRevIdFetched() {
391 if ( $this->mRevIdFetched ) {
392 return $this->mRevIdFetched;
393 } else {
394 return $this->mPage->getLatest();
395 }
396 }
397
398 /**
399 * This is the default action of the index.php entry point: just view the
400 * page of the given title.
401 */
402 public function view() {
403 global $wgUser, $wgOut, $wgRequest, $wgParser;
404 global $wgUseFileCache, $wgUseETag, $wgDebugToolbar;
405
406 wfProfileIn( __METHOD__ );
407
408 # Get variables from query string
409 # As side effect this will load the revision and update the title
410 # in a revision ID is passed in the request, so this should remain
411 # the first call of this method even if $oldid is used way below.
412 $oldid = $this->getOldID();
413
414 # Another whitelist check in case getOldID() is altering the title
415 $permErrors = $this->getTitle()->getUserPermissionsErrors( 'read', $wgUser );
416 if ( count( $permErrors ) ) {
417 wfDebug( __METHOD__ . ": denied on secondary read check\n" );
418 wfProfileOut( __METHOD__ );
419 throw new PermissionsError( 'read', $permErrors );
420 }
421
422 # getOldID() may as well want us to redirect somewhere else
423 if ( $this->mRedirectUrl ) {
424 $wgOut->redirect( $this->mRedirectUrl );
425 wfDebug( __METHOD__ . ": redirecting due to oldid\n" );
426 wfProfileOut( __METHOD__ );
427
428 return;
429 }
430
431 # If we got diff in the query, we want to see a diff page instead of the article.
432 if ( $wgRequest->getCheck( 'diff' ) ) {
433 wfDebug( __METHOD__ . ": showing diff page\n" );
434 $this->showDiffPage();
435 wfProfileOut( __METHOD__ );
436
437 return;
438 }
439
440 # Set page title (may be overridden by DISPLAYTITLE)
441 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
442
443 $wgOut->setArticleFlag( true );
444 # Allow frames by default
445 $wgOut->allowClickjacking();
446
447 $parserCache = ParserCache::singleton();
448
449 $parserOptions = $this->getParserOptions();
450 # Render printable version, use printable version cache
451 if ( $wgOut->isPrintable() ) {
452 $parserOptions->setIsPrintable( true );
453 $parserOptions->setEditSection( false );
454 } elseif ( !$this->getTitle()->quickUserCan( 'edit' ) ) {
455 $parserOptions->setEditSection( false );
456 }
457
458 # Try client and file cache
459 if ( !$wgDebugToolbar && $oldid === 0 && $this->mPage->checkTouched() ) {
460 if ( $wgUseETag ) {
461 $wgOut->setETag( $parserCache->getETag( $this, $parserOptions ) );
462 }
463
464 # Is it client cached?
465 if ( $wgOut->checkLastModified( $this->mPage->getTouched() ) ) {
466 wfDebug( __METHOD__ . ": done 304\n" );
467 wfProfileOut( __METHOD__ );
468
469 return;
470 # Try file cache
471 } elseif ( $wgUseFileCache && $this->tryFileCache() ) {
472 wfDebug( __METHOD__ . ": done file cache\n" );
473 # tell wgOut that output is taken care of
474 $wgOut->disable();
475 $this->mPage->doViewUpdates( $wgUser );
476 wfProfileOut( __METHOD__ );
477
478 return;
479 }
480 }
481
482 # Should the parser cache be used?
483 $useParserCache = $this->mPage->isParserCacheUsed( $parserOptions, $oldid );
484 wfDebug( 'Article::view using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" );
485 if ( $wgUser->getStubThreshold() ) {
486 wfIncrStats( 'pcache_miss_stub' );
487 }
488
489 $this->showRedirectedFromHeader();
490 $this->showNamespaceHeader();
491
492 # Iterate through the possible ways of constructing the output text.
493 # Keep going until $outputDone is set, or we run out of things to do.
494 $pass = 0;
495 $outputDone = false;
496 $this->mParserOutput = false;
497
498 while ( !$outputDone && ++$pass ) {
499 switch( $pass ) {
500 case 1:
501 wfRunHooks( 'ArticleViewHeader', array( &$this, &$outputDone, &$useParserCache ) );
502 break;
503 case 2:
504 # Early abort if the page doesn't exist
505 if ( !$this->mPage->exists() ) {
506 wfDebug( __METHOD__ . ": showing missing article\n" );
507 $this->showMissingArticle();
508 wfProfileOut( __METHOD__ );
509 return;
510 }
511
512 # Try the parser cache
513 if ( $useParserCache ) {
514 $this->mParserOutput = $parserCache->get( $this, $parserOptions );
515
516 if ( $this->mParserOutput !== false ) {
517 if ( $oldid ) {
518 wfDebug( __METHOD__ . ": showing parser cache contents for current rev permalink\n" );
519 $this->setOldSubtitle( $oldid );
520 } else {
521 wfDebug( __METHOD__ . ": showing parser cache contents\n" );
522 }
523 $wgOut->addParserOutput( $this->mParserOutput );
524 # Ensure that UI elements requiring revision ID have
525 # the correct version information.
526 $wgOut->setRevisionId( $this->mPage->getLatest() );
527 # Preload timestamp to avoid a DB hit
528 $cachedTimestamp = $this->mParserOutput->getTimestamp();
529 if ( $cachedTimestamp !== null ) {
530 $wgOut->setRevisionTimestamp( $cachedTimestamp );
531 $this->mPage->setTimestamp( $cachedTimestamp );
532 }
533 $outputDone = true;
534 }
535 }
536 break;
537 case 3:
538 # This will set $this->mRevision if needed
539 $this->fetchContent();
540
541 # Are we looking at an old revision
542 if ( $oldid && $this->mRevision ) {
543 $this->setOldSubtitle( $oldid );
544
545 if ( !$this->showDeletedRevisionHeader() ) {
546 wfDebug( __METHOD__ . ": cannot view deleted revision\n" );
547 wfProfileOut( __METHOD__ );
548 return;
549 }
550 }
551
552 # Ensure that UI elements requiring revision ID have
553 # the correct version information.
554 $wgOut->setRevisionId( $this->getRevIdFetched() );
555 # Preload timestamp to avoid a DB hit
556 $wgOut->setRevisionTimestamp( $this->getTimestamp() );
557
558 # Pages containing custom CSS or JavaScript get special treatment
559 if ( $this->getTitle()->isCssOrJsPage() || $this->getTitle()->isCssJsSubpage() ) {
560 #FIXME: use Content object instead!
561 wfDebug( __METHOD__ . ": showing CSS/JS source\n" );
562 $this->showCssOrJsPage();
563 $outputDone = true;
564 } elseif( !wfRunHooks( 'ArticleViewCustom', array( $this->mContent, $this->getTitle(), $wgOut ) ) ) {
565 # Allow extensions do their own custom view for certain pages
566 $outputDone = true;
567 } else {
568 $text = $this->getContent();
569 $rt = Title::newFromRedirectArray( $text );
570 if ( $rt ) {
571 wfDebug( __METHOD__ . ": showing redirect=no page\n" );
572 # Viewing a redirect page (e.g. with parameter redirect=no)
573 $wgOut->addHTML( $this->viewRedirect( $rt ) );
574 # Parse just to get categories, displaytitle, etc.
575 $this->mParserOutput = $wgParser->parse( $text, $this->getTitle(), $parserOptions );
576 $wgOut->addParserOutputNoText( $this->mParserOutput );
577 $outputDone = true;
578 }
579 }
580 break;
581 case 4:
582 # Run the parse, protected by a pool counter
583 wfDebug( __METHOD__ . ": doing uncached parse\n" );
584
585 $poolArticleView = new PoolWorkArticleView( $this, $parserOptions,
586 $this->getRevIdFetched(), $useParserCache, $this->getContent() );
587
588 if ( !$poolArticleView->execute() ) {
589 $error = $poolArticleView->getError();
590 if ( $error ) {
591 $wgOut->clearHTML(); // for release() errors
592 $wgOut->enableClientCache( false );
593 $wgOut->setRobotPolicy( 'noindex,nofollow' );
594
595 $errortext = $error->getWikiText( false, 'view-pool-error' );
596 $wgOut->addWikiText( '<div class="errorbox">' . $errortext . '</div>' );
597 }
598 # Connection or timeout error
599 wfProfileOut( __METHOD__ );
600 return;
601 }
602
603 $this->mParserOutput = $poolArticleView->getParserOutput();
604 $wgOut->addParserOutput( $this->mParserOutput );
605
606 # Don't cache a dirty ParserOutput object
607 if ( $poolArticleView->getIsDirty() ) {
608 $wgOut->setSquidMaxage( 0 );
609 $wgOut->addHTML( "<!-- parser cache is expired, sending anyway due to pool overload-->\n" );
610 }
611
612 $outputDone = true;
613 break;
614 # Should be unreachable, but just in case...
615 default:
616 break 2;
617 }
618 }
619
620 # Get the ParserOutput actually *displayed* here.
621 # Note that $this->mParserOutput is the *current* version output.
622 $pOutput = ( $outputDone instanceof ParserOutput )
623 ? $outputDone // object fetched by hook
624 : $this->mParserOutput;
625
626 # Adjust title for main page & pages with displaytitle
627 if ( $pOutput ) {
628 $this->adjustDisplayTitle( $pOutput );
629 }
630
631 # For the main page, overwrite the <title> element with the con-
632 # tents of 'pagetitle-view-mainpage' instead of the default (if
633 # that's not empty).
634 # This message always exists because it is in the i18n files
635 if ( $this->getTitle()->isMainPage() ) {
636 $msg = wfMessage( 'pagetitle-view-mainpage' )->inContentLanguage();
637 if ( !$msg->isDisabled() ) {
638 $wgOut->setHTMLTitle( $msg->title( $this->getTitle() )->text() );
639 }
640 }
641
642 # Check for any __NOINDEX__ tags on the page using $pOutput
643 $policy = $this->getRobotPolicy( 'view', $pOutput );
644 $wgOut->setIndexPolicy( $policy['index'] );
645 $wgOut->setFollowPolicy( $policy['follow'] );
646
647 $this->showViewFooter();
648 $this->mPage->doViewUpdates( $wgUser );
649
650 wfProfileOut( __METHOD__ );
651 }
652
653 /**
654 * Adjust title for pages with displaytitle, -{T|}- or language conversion
655 * @param $pOutput ParserOutput
656 */
657 public function adjustDisplayTitle( ParserOutput $pOutput ) {
658 global $wgOut;
659 # Adjust the title if it was set by displaytitle, -{T|}- or language conversion
660 $titleText = $pOutput->getTitleText();
661 if ( strval( $titleText ) !== '' ) {
662 $wgOut->setPageTitle( $titleText );
663 }
664 }
665
666 /**
667 * Show a diff page according to current request variables. For use within
668 * Article::view() only, other callers should use the DifferenceEngine class.
669 */
670 public function showDiffPage() {
671 global $wgRequest, $wgUser;
672
673 $diff = $wgRequest->getVal( 'diff' );
674 $rcid = $wgRequest->getVal( 'rcid' );
675 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
676 $purge = $wgRequest->getVal( 'action' ) == 'purge';
677 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
678 $oldid = $this->getOldID();
679
680 $de = new DifferenceEngine( $this->getContext(), $oldid, $diff, $rcid, $purge, $unhide );
681 // DifferenceEngine directly fetched the revision:
682 $this->mRevIdFetched = $de->mNewid;
683 $de->showDiffPage( $diffOnly );
684
685 if ( $diff == 0 || $diff == $this->mPage->getLatest() ) {
686 # Run view updates for current revision only
687 $this->mPage->doViewUpdates( $wgUser );
688 }
689 }
690
691 /**
692 * Show a page view for a page formatted as CSS or JavaScript. To be called by
693 * Article::view() only.
694 *
695 * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these
696 * page views.
697 */
698 protected function showCssOrJsPage() { #FIXME: deprecate, keep for BC
699 global $wgOut;
700
701 $dir = $this->getContext()->getLanguage()->getDir();
702 $lang = $this->getContext()->getLanguage()->getCode();
703
704 $wgOut->wrapWikiMsg( "<div id='mw-clearyourcache' lang='$lang' dir='$dir' class='mw-content-$dir'>\n$1\n</div>",
705 'clearyourcache' ); #FIXME: get this from handler
706
707 // Give hooks a chance to customise the output
708 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mContent, $this->getTitle(), $wgOut ) ) ) {
709 #FIXME: use content object instead
710 // Wrap the whole lot in a <pre> and don't parse
711 $m = array();
712 preg_match( '!\.(css|js)$!u', $this->getTitle()->getText(), $m );
713 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
714 $wgOut->addHTML( htmlspecialchars( $this->mContent ) );
715 $wgOut->addHTML( "\n</pre>\n" );
716 }
717 }
718
719 /**
720 * Get the robot policy to be used for the current view
721 * @param $action String the action= GET parameter
722 * @param $pOutput ParserOutput
723 * @return Array the policy that should be set
724 * TODO: actions other than 'view'
725 */
726 public function getRobotPolicy( $action, $pOutput ) {
727 global $wgOut, $wgArticleRobotPolicies, $wgNamespaceRobotPolicies;
728 global $wgDefaultRobotPolicy, $wgRequest;
729
730 $ns = $this->getTitle()->getNamespace();
731
732 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
733 # Don't index user and user talk pages for blocked users (bug 11443)
734 if ( !$this->getTitle()->isSubpage() ) {
735 if ( Block::newFromTarget( null, $this->getTitle()->getText() ) instanceof Block ) {
736 return array(
737 'index' => 'noindex',
738 'follow' => 'nofollow'
739 );
740 }
741 }
742 }
743
744 if ( $this->mPage->getID() === 0 || $this->getOldID() ) {
745 # Non-articles (special pages etc), and old revisions
746 return array(
747 'index' => 'noindex',
748 'follow' => 'nofollow'
749 );
750 } elseif ( $wgOut->isPrintable() ) {
751 # Discourage indexing of printable versions, but encourage following
752 return array(
753 'index' => 'noindex',
754 'follow' => 'follow'
755 );
756 } elseif ( $wgRequest->getInt( 'curid' ) ) {
757 # For ?curid=x urls, disallow indexing
758 return array(
759 'index' => 'noindex',
760 'follow' => 'follow'
761 );
762 }
763
764 # Otherwise, construct the policy based on the various config variables.
765 $policy = self::formatRobotPolicy( $wgDefaultRobotPolicy );
766
767 if ( isset( $wgNamespaceRobotPolicies[$ns] ) ) {
768 # Honour customised robot policies for this namespace
769 $policy = array_merge(
770 $policy,
771 self::formatRobotPolicy( $wgNamespaceRobotPolicies[$ns] )
772 );
773 }
774 if ( $this->getTitle()->canUseNoindex() && is_object( $pOutput ) && $pOutput->getIndexPolicy() ) {
775 # __INDEX__ and __NOINDEX__ magic words, if allowed. Incorporates
776 # a final sanity check that we have really got the parser output.
777 $policy = array_merge(
778 $policy,
779 array( 'index' => $pOutput->getIndexPolicy() )
780 );
781 }
782
783 if ( isset( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] ) ) {
784 # (bug 14900) site config can override user-defined __INDEX__ or __NOINDEX__
785 $policy = array_merge(
786 $policy,
787 self::formatRobotPolicy( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] )
788 );
789 }
790
791 return $policy;
792 }
793
794 /**
795 * Converts a String robot policy into an associative array, to allow
796 * merging of several policies using array_merge().
797 * @param $policy Mixed, returns empty array on null/false/'', transparent
798 * to already-converted arrays, converts String.
799 * @return Array: 'index' => <indexpolicy>, 'follow' => <followpolicy>
800 */
801 public static function formatRobotPolicy( $policy ) {
802 if ( is_array( $policy ) ) {
803 return $policy;
804 } elseif ( !$policy ) {
805 return array();
806 }
807
808 $policy = explode( ',', $policy );
809 $policy = array_map( 'trim', $policy );
810
811 $arr = array();
812 foreach ( $policy as $var ) {
813 if ( in_array( $var, array( 'index', 'noindex' ) ) ) {
814 $arr['index'] = $var;
815 } elseif ( in_array( $var, array( 'follow', 'nofollow' ) ) ) {
816 $arr['follow'] = $var;
817 }
818 }
819
820 return $arr;
821 }
822
823 /**
824 * If this request is a redirect view, send "redirected from" subtitle to
825 * $wgOut. Returns true if the header was needed, false if this is not a
826 * redirect view. Handles both local and remote redirects.
827 *
828 * @return boolean
829 */
830 public function showRedirectedFromHeader() {
831 global $wgOut, $wgRequest, $wgRedirectSources;
832
833 $rdfrom = $wgRequest->getVal( 'rdfrom' );
834
835 if ( isset( $this->mRedirectedFrom ) ) {
836 // This is an internally redirected page view.
837 // We'll need a backlink to the source page for navigation.
838 if ( wfRunHooks( 'ArticleViewRedirect', array( &$this ) ) ) {
839 $redir = Linker::linkKnown(
840 $this->mRedirectedFrom,
841 null,
842 array(),
843 array( 'redirect' => 'no' )
844 );
845
846 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
847
848 // Set the fragment if one was specified in the redirect
849 if ( strval( $this->getTitle()->getFragment() ) != '' ) {
850 $fragment = Xml::escapeJsString( $this->getTitle()->getFragmentForURL() );
851 $wgOut->addInlineScript( "redirectToFragment(\"$fragment\");" );
852 }
853
854 // Add a <link rel="canonical"> tag
855 $wgOut->addLink( array( 'rel' => 'canonical',
856 'href' => $this->getTitle()->getLocalURL() )
857 );
858
859 // Tell $wgOut the user arrived at this article through a redirect
860 $wgOut->setRedirectedFrom( $this->mRedirectedFrom );
861
862 return true;
863 }
864 } elseif ( $rdfrom ) {
865 // This is an externally redirected view, from some other wiki.
866 // If it was reported from a trusted site, supply a backlink.
867 if ( $wgRedirectSources && preg_match( $wgRedirectSources, $rdfrom ) ) {
868 $redir = Linker::makeExternalLink( $rdfrom, $rdfrom );
869 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
870
871 return true;
872 }
873 }
874
875 return false;
876 }
877
878 /**
879 * Show a header specific to the namespace currently being viewed, like
880 * [[MediaWiki:Talkpagetext]]. For Article::view().
881 */
882 public function showNamespaceHeader() {
883 global $wgOut;
884
885 if ( $this->getTitle()->isTalkPage() ) {
886 if ( !wfMessage( 'talkpageheader' )->isDisabled() ) {
887 $wgOut->wrapWikiMsg( "<div class=\"mw-talkpageheader\">\n$1\n</div>", array( 'talkpageheader' ) );
888 }
889 }
890 }
891
892 /**
893 * Show the footer section of an ordinary page view
894 */
895 public function showViewFooter() {
896 global $wgOut;
897
898 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
899 if ( $this->getTitle()->getNamespace() == NS_USER_TALK && IP::isValid( $this->getTitle()->getText() ) ) {
900 $wgOut->addWikiMsg( 'anontalkpagetext' );
901 }
902
903 # If we have been passed an &rcid= parameter, we want to give the user a
904 # chance to mark this new article as patrolled.
905 $this->showPatrolFooter();
906
907 wfRunHooks( 'ArticleViewFooter', array( $this ) );
908
909 }
910
911 /**
912 * If patrol is possible, output a patrol UI box. This is called from the
913 * footer section of ordinary page views. If patrol is not possible or not
914 * desired, does nothing.
915 */
916 public function showPatrolFooter() {
917 global $wgOut, $wgRequest, $wgUser;
918
919 $rcid = $wgRequest->getVal( 'rcid' );
920
921 if ( !$rcid || !$this->getTitle()->quickUserCan( 'patrol' ) ) {
922 return;
923 }
924
925 $token = $wgUser->getEditToken( $rcid );
926 $wgOut->preventClickjacking();
927
928 $wgOut->addHTML(
929 "<div class='patrollink'>" .
930 wfMsgHtml(
931 'markaspatrolledlink',
932 Linker::link(
933 $this->getTitle(),
934 wfMsgHtml( 'markaspatrolledtext' ),
935 array(),
936 array(
937 'action' => 'markpatrolled',
938 'rcid' => $rcid,
939 'token' => $token,
940 ),
941 array( 'known', 'noclasses' )
942 )
943 ) .
944 '</div>'
945 );
946 }
947
948 /**
949 * Show the error text for a missing article. For articles in the MediaWiki
950 * namespace, show the default message text. To be called from Article::view().
951 */
952 public function showMissingArticle() {
953 global $wgOut, $wgRequest, $wgUser, $wgSend404Code;
954
955 # Show info in user (talk) namespace. Does the user exist? Is he blocked?
956 if ( $this->getTitle()->getNamespace() == NS_USER || $this->getTitle()->getNamespace() == NS_USER_TALK ) {
957 $parts = explode( '/', $this->getTitle()->getText() );
958 $rootPart = $parts[0];
959 $user = User::newFromName( $rootPart, false /* allow IP users*/ );
960 $ip = User::isIP( $rootPart );
961
962 if ( !($user && $user->isLoggedIn()) && !$ip ) { # User does not exist
963 $wgOut->wrapWikiMsg( "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
964 array( 'userpage-userdoesnotexist-view', wfEscapeWikiText( $rootPart ) ) );
965 } elseif ( $user->isBlocked() ) { # Show log extract if the user is currently blocked
966 LogEventsList::showLogExtract(
967 $wgOut,
968 'block',
969 $user->getUserPage()->getPrefixedText(),
970 '',
971 array(
972 'lim' => 1,
973 'showIfEmpty' => false,
974 'msgKey' => array(
975 'blocked-notice-logextract',
976 $user->getName() # Support GENDER in notice
977 )
978 )
979 );
980 }
981 }
982
983 wfRunHooks( 'ShowMissingArticle', array( $this ) );
984
985 # Show delete and move logs
986 LogEventsList::showLogExtract( $wgOut, array( 'delete', 'move' ), $this->getTitle()->getPrefixedText(), '',
987 array( 'lim' => 10,
988 'conds' => array( "log_action != 'revision'" ),
989 'showIfEmpty' => false,
990 'msgKey' => array( 'moveddeleted-notice' ) )
991 );
992
993 # Show error message
994 $oldid = $this->getOldID();
995 if ( $oldid ) {
996 $text = wfMsgNoTrans( 'missing-article',
997 $this->getTitle()->getPrefixedText(),
998 wfMsgNoTrans( 'missingarticle-rev', $oldid ) );
999 } elseif ( $this->getTitle()->getNamespace() === NS_MEDIAWIKI ) {
1000 // Use the default message text
1001 $text = $this->getTitle()->getDefaultMessageText();
1002 } else {
1003 $createErrors = $this->getTitle()->getUserPermissionsErrors( 'create', $wgUser );
1004 $editErrors = $this->getTitle()->getUserPermissionsErrors( 'edit', $wgUser );
1005 $errors = array_merge( $createErrors, $editErrors );
1006
1007 if ( !count( $errors ) ) {
1008 $text = wfMsgNoTrans( 'noarticletext' );
1009 } else {
1010 $text = wfMsgNoTrans( 'noarticletext-nopermission' );
1011 }
1012 }
1013 $text = "<div class='noarticletext'>\n$text\n</div>";
1014
1015 if ( !$this->mPage->hasViewableContent() && $wgSend404Code ) {
1016 // If there's no backing content, send a 404 Not Found
1017 // for better machine handling of broken links.
1018 $wgRequest->response()->header( "HTTP/1.1 404 Not Found" );
1019 }
1020
1021 $wgOut->addWikiText( $text );
1022 }
1023
1024 /**
1025 * If the revision requested for view is deleted, check permissions.
1026 * Send either an error message or a warning header to $wgOut.
1027 *
1028 * @return boolean true if the view is allowed, false if not.
1029 */
1030 public function showDeletedRevisionHeader() {
1031 global $wgOut, $wgRequest;
1032
1033 if ( !$this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
1034 // Not deleted
1035 return true;
1036 }
1037
1038 // If the user is not allowed to see it...
1039 if ( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
1040 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1041 'rev-deleted-text-permission' );
1042
1043 return false;
1044 // If the user needs to confirm that they want to see it...
1045 } elseif ( $wgRequest->getInt( 'unhide' ) != 1 ) {
1046 # Give explanation and add a link to view the revision...
1047 $oldid = intval( $this->getOldID() );
1048 $link = $this->getTitle()->getFullUrl( "oldid={$oldid}&unhide=1" );
1049 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1050 'rev-suppressed-text-unhide' : 'rev-deleted-text-unhide';
1051 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1052 array( $msg, $link ) );
1053
1054 return false;
1055 // We are allowed to see...
1056 } else {
1057 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1058 'rev-suppressed-text-view' : 'rev-deleted-text-view';
1059 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", $msg );
1060
1061 return true;
1062 }
1063 }
1064
1065 /**
1066 * Generate the navigation links when browsing through an article revisions
1067 * It shows the information as:
1068 * Revision as of \<date\>; view current revision
1069 * \<- Previous version | Next Version -\>
1070 *
1071 * @param $oldid String: revision ID of this article revision
1072 */
1073 public function setOldSubtitle( $oldid = 0 ) {
1074 global $wgLang, $wgOut, $wgUser, $wgRequest;
1075
1076 if ( !wfRunHooks( 'DisplayOldSubtitle', array( &$this, &$oldid ) ) ) {
1077 return;
1078 }
1079
1080 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
1081
1082 # Cascade unhide param in links for easy deletion browsing
1083 $extraParams = array();
1084 if ( $wgRequest->getVal( 'unhide' ) ) {
1085 $extraParams['unhide'] = 1;
1086 }
1087
1088 $revision = Revision::newFromId( $oldid );
1089 $timestamp = $revision->getTimestamp();
1090
1091 $current = ( $oldid == $this->mPage->getLatest() );
1092 $td = $wgLang->timeanddate( $timestamp, true );
1093 $tddate = $wgLang->date( $timestamp, true );
1094 $tdtime = $wgLang->time( $timestamp, true );
1095
1096 # Show user links if allowed to see them. If hidden, then show them only if requested...
1097 $userlinks = Linker::revUserTools( $revision, !$unhide );
1098
1099 $infomsg = $current && !wfMessage( 'revision-info-current' )->isDisabled()
1100 ? 'revision-info-current'
1101 : 'revision-info';
1102
1103 $wgOut->addSubtitle( "<div id=\"mw-{$infomsg}\">" . wfMessage( $infomsg,
1104 $td )->rawParams( $userlinks )->params( $revision->getID(), $tddate,
1105 $tdtime, $revision->getUser() )->parse() . "</div>" );
1106
1107 $lnk = $current
1108 ? wfMsgHtml( 'currentrevisionlink' )
1109 : Linker::link(
1110 $this->getTitle(),
1111 wfMsgHtml( 'currentrevisionlink' ),
1112 array(),
1113 $extraParams,
1114 array( 'known', 'noclasses' )
1115 );
1116 $curdiff = $current
1117 ? wfMsgHtml( 'diff' )
1118 : Linker::link(
1119 $this->getTitle(),
1120 wfMsgHtml( 'diff' ),
1121 array(),
1122 array(
1123 'diff' => 'cur',
1124 'oldid' => $oldid
1125 ) + $extraParams,
1126 array( 'known', 'noclasses' )
1127 );
1128 $prev = $this->getTitle()->getPreviousRevisionID( $oldid ) ;
1129 $prevlink = $prev
1130 ? Linker::link(
1131 $this->getTitle(),
1132 wfMsgHtml( 'previousrevision' ),
1133 array(),
1134 array(
1135 'direction' => 'prev',
1136 'oldid' => $oldid
1137 ) + $extraParams,
1138 array( 'known', 'noclasses' )
1139 )
1140 : wfMsgHtml( 'previousrevision' );
1141 $prevdiff = $prev
1142 ? Linker::link(
1143 $this->getTitle(),
1144 wfMsgHtml( 'diff' ),
1145 array(),
1146 array(
1147 'diff' => 'prev',
1148 'oldid' => $oldid
1149 ) + $extraParams,
1150 array( 'known', 'noclasses' )
1151 )
1152 : wfMsgHtml( 'diff' );
1153 $nextlink = $current
1154 ? wfMsgHtml( 'nextrevision' )
1155 : Linker::link(
1156 $this->getTitle(),
1157 wfMsgHtml( 'nextrevision' ),
1158 array(),
1159 array(
1160 'direction' => 'next',
1161 'oldid' => $oldid
1162 ) + $extraParams,
1163 array( 'known', 'noclasses' )
1164 );
1165 $nextdiff = $current
1166 ? wfMsgHtml( 'diff' )
1167 : Linker::link(
1168 $this->getTitle(),
1169 wfMsgHtml( 'diff' ),
1170 array(),
1171 array(
1172 'diff' => 'next',
1173 'oldid' => $oldid
1174 ) + $extraParams,
1175 array( 'known', 'noclasses' )
1176 );
1177
1178 $cdel = Linker::getRevDeleteLink( $wgUser, $revision, $this->getTitle() );
1179 if ( $cdel !== '' ) {
1180 $cdel .= ' ';
1181 }
1182
1183 $wgOut->addSubtitle( "<div id=\"mw-revision-nav\">" . $cdel .
1184 wfMsgExt( 'revision-nav', array( 'escapenoentities', 'parsemag', 'replaceafter' ),
1185 $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>" );
1186 }
1187
1188 /**
1189 * View redirect
1190 *
1191 * @param $target Title|Array of destination(s) to redirect
1192 * @param $appendSubtitle Boolean [optional]
1193 * @param $forceKnown Boolean: should the image be shown as a bluelink regardless of existence?
1194 * @return string containing HMTL with redirect link
1195 */
1196 public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
1197 global $wgOut, $wgStylePath;
1198
1199 if ( !is_array( $target ) ) {
1200 $target = array( $target );
1201 }
1202
1203 $lang = $this->getTitle()->getPageLanguage();
1204 $imageDir = $lang->getDir();
1205
1206 if ( $appendSubtitle ) {
1207 $wgOut->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
1208 }
1209
1210 // the loop prepends the arrow image before the link, so the first case needs to be outside
1211
1212 /**
1213 * @var $title Title
1214 */
1215 $title = array_shift( $target );
1216
1217 if ( $forceKnown ) {
1218 $link = Linker::linkKnown( $title, htmlspecialchars( $title->getFullText() ) );
1219 } else {
1220 $link = Linker::link( $title, htmlspecialchars( $title->getFullText() ) );
1221 }
1222
1223 $nextRedirect = $wgStylePath . '/common/images/nextredirect' . $imageDir . '.png';
1224 $alt = $lang->isRTL() ? '←' : '→';
1225 // Automatically append redirect=no to each link, since most of them are redirect pages themselves.
1226 foreach ( $target as $rt ) {
1227 $link .= Html::element( 'img', array( 'src' => $nextRedirect, 'alt' => $alt ) );
1228 if ( $forceKnown ) {
1229 $link .= Linker::linkKnown( $rt, htmlspecialchars( $rt->getFullText(), array(), array( 'redirect' => 'no' ) ) );
1230 } else {
1231 $link .= Linker::link( $rt, htmlspecialchars( $rt->getFullText() ), array(), array( 'redirect' => 'no' ) );
1232 }
1233 }
1234
1235 $imageUrl = $wgStylePath . '/common/images/redirect' . $imageDir . '.png';
1236 return '<div class="redirectMsg">' .
1237 Html::element( 'img', array( 'src' => $imageUrl, 'alt' => '#REDIRECT' ) ) .
1238 '<span class="redirectText">' . $link . '</span></div>';
1239 }
1240
1241 /**
1242 * Handle action=render
1243 */
1244 public function render() {
1245 global $wgOut;
1246
1247 $wgOut->setArticleBodyOnly( true );
1248 $this->view();
1249 }
1250
1251 /**
1252 * action=protect handler
1253 */
1254 public function protect() {
1255 $form = new ProtectionForm( $this );
1256 $form->execute();
1257 }
1258
1259 /**
1260 * action=unprotect handler (alias)
1261 */
1262 public function unprotect() {
1263 $this->protect();
1264 }
1265
1266 /**
1267 * UI entry point for page deletion
1268 */
1269 public function delete() {
1270 global $wgOut, $wgRequest, $wgLang;
1271
1272 # This code desperately needs to be totally rewritten
1273
1274 $title = $this->getTitle();
1275 $user = $this->getContext()->getUser();
1276
1277 # Check permissions
1278 $permission_errors = $title->getUserPermissionsErrors( 'delete', $user );
1279 if ( count( $permission_errors ) ) {
1280 throw new PermissionsError( 'delete', $permission_errors );
1281 }
1282
1283 # Read-only check...
1284 if ( wfReadOnly() ) {
1285 throw new ReadOnlyError;
1286 }
1287
1288 # Better double-check that it hasn't been deleted yet!
1289 $dbw = wfGetDB( DB_MASTER );
1290 $conds = $title->pageCond();
1291 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
1292 if ( $latest === false ) {
1293 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $title->getPrefixedText() ) );
1294 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1295 array( 'cannotdelete', wfEscapeWikiText( $title->getPrefixedText() ) )
1296 );
1297 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1298 LogEventsList::showLogExtract(
1299 $wgOut,
1300 'delete',
1301 $title->getPrefixedText()
1302 );
1303
1304 return;
1305 }
1306
1307 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' );
1308 $deleteReason = $wgRequest->getText( 'wpReason' );
1309
1310 if ( $deleteReasonList == 'other' ) {
1311 $reason = $deleteReason;
1312 } elseif ( $deleteReason != '' ) {
1313 // Entry from drop down menu + additional comment
1314 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
1315 } else {
1316 $reason = $deleteReasonList;
1317 }
1318
1319 if ( $wgRequest->wasPosted() && $user->matchEditToken( $wgRequest->getVal( 'wpEditToken' ),
1320 array( 'delete', $this->getTitle()->getPrefixedText() ) ) )
1321 {
1322 # Flag to hide all contents of the archived revisions
1323 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $user->isAllowed( 'suppressrevision' );
1324
1325 $this->doDelete( $reason, $suppress );
1326
1327 if ( $wgRequest->getCheck( 'wpWatch' ) && $user->isLoggedIn() ) {
1328 $this->doWatch();
1329 } elseif ( $title->userIsWatching() ) {
1330 $this->doUnwatch();
1331 }
1332
1333 return;
1334 }
1335
1336 // Generate deletion reason
1337 $hasHistory = false;
1338 if ( !$reason ) {
1339 $reason = $this->generateReason( $hasHistory );
1340 }
1341
1342 // If the page has a history, insert a warning
1343 if ( $hasHistory ) {
1344 $revisions = $this->mTitle->estimateRevisionCount();
1345 // @todo FIXME: i18n issue/patchwork message
1346 $wgOut->addHTML( '<strong class="mw-delete-warning-revisions">' .
1347 wfMsgExt( 'historywarning', array( 'parseinline' ), $wgLang->formatNum( $revisions ) ) .
1348 wfMsgHtml( 'word-separator' ) . Linker::link( $title,
1349 wfMsgHtml( 'history' ),
1350 array( 'rel' => 'archives' ),
1351 array( 'action' => 'history' ) ) .
1352 '</strong>'
1353 );
1354
1355 if ( $this->mTitle->isBigDeletion() ) {
1356 global $wgDeleteRevisionsLimit;
1357 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n",
1358 array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
1359 }
1360 }
1361
1362 return $this->confirmDelete( $reason );
1363 }
1364
1365 /**
1366 * Output deletion confirmation dialog
1367 * @todo FIXME: Move to another file?
1368 * @param $reason String: prefilled reason
1369 */
1370 public function confirmDelete( $reason ) {
1371 global $wgOut;
1372
1373 wfDebug( "Article::confirmDelete\n" );
1374
1375 $wgOut->setPageTitle( wfMessage( 'delete-confirm', $this->getTitle()->getPrefixedText() ) );
1376 $wgOut->addBacklinkSubtitle( $this->getTitle() );
1377 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1378 $wgOut->addWikiMsg( 'confirmdeletetext' );
1379
1380 wfRunHooks( 'ArticleConfirmDelete', array( $this, $wgOut, &$reason ) );
1381
1382 $user = $this->getContext()->getUser();
1383
1384 if ( $user->isAllowed( 'suppressrevision' ) ) {
1385 $suppress = "<tr id=\"wpDeleteSuppressRow\">
1386 <td></td>
1387 <td class='mw-input'><strong>" .
1388 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
1389 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '4' ) ) .
1390 "</strong></td>
1391 </tr>";
1392 } else {
1393 $suppress = '';
1394 }
1395 $checkWatch = $user->getBoolOption( 'watchdeletion' ) || $this->getTitle()->userIsWatching();
1396
1397 $form = Xml::openElement( 'form', array( 'method' => 'post',
1398 'action' => $this->getTitle()->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
1399 Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
1400 Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
1401 Xml::openElement( 'table', array( 'id' => 'mw-deleteconfirm-table' ) ) .
1402 "<tr id=\"wpDeleteReasonListRow\">
1403 <td class='mw-label'>" .
1404 Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
1405 "</td>
1406 <td class='mw-input'>" .
1407 Xml::listDropDown( 'wpDeleteReasonList',
1408 wfMsgForContent( 'deletereason-dropdown' ),
1409 wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
1410 "</td>
1411 </tr>
1412 <tr id=\"wpDeleteReasonRow\">
1413 <td class='mw-label'>" .
1414 Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
1415 "</td>
1416 <td class='mw-input'>" .
1417 Html::input( 'wpReason', $reason, 'text', array(
1418 'size' => '60',
1419 'maxlength' => '255',
1420 'tabindex' => '2',
1421 'id' => 'wpReason',
1422 'autofocus'
1423 ) ) .
1424 "</td>
1425 </tr>";
1426
1427 # Disallow watching if user is not logged in
1428 if ( $user->isLoggedIn() ) {
1429 $form .= "
1430 <tr>
1431 <td></td>
1432 <td class='mw-input'>" .
1433 Xml::checkLabel( wfMsg( 'watchthis' ),
1434 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
1435 "</td>
1436 </tr>";
1437 }
1438
1439 $form .= "
1440 $suppress
1441 <tr>
1442 <td></td>
1443 <td class='mw-submit'>" .
1444 Xml::submitButton( wfMsg( 'deletepage' ),
1445 array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '5' ) ) .
1446 "</td>
1447 </tr>" .
1448 Xml::closeElement( 'table' ) .
1449 Xml::closeElement( 'fieldset' ) .
1450 Html::hidden( 'wpEditToken', $user->getEditToken( array( 'delete', $this->getTitle()->getPrefixedText() ) ) ) .
1451 Xml::closeElement( 'form' );
1452
1453 if ( $user->isAllowed( 'editinterface' ) ) {
1454 $title = Title::makeTitle( NS_MEDIAWIKI, 'Deletereason-dropdown' );
1455 $link = Linker::link(
1456 $title,
1457 wfMsgHtml( 'delete-edit-reasonlist' ),
1458 array(),
1459 array( 'action' => 'edit' )
1460 );
1461 $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
1462 }
1463
1464 $wgOut->addHTML( $form );
1465 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1466 LogEventsList::showLogExtract( $wgOut, 'delete',
1467 $this->getTitle()->getPrefixedText()
1468 );
1469 }
1470
1471 /**
1472 * Perform a deletion and output success or failure messages
1473 * @param $reason
1474 * @param $suppress bool
1475 */
1476 public function doDelete( $reason, $suppress = false ) {
1477 global $wgOut;
1478
1479 $error = '';
1480 if ( $this->mPage->doDeleteArticle( $reason, $suppress, 0, true, $error ) ) {
1481 $deleted = $this->getTitle()->getPrefixedText();
1482
1483 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
1484 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1485
1486 $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
1487
1488 $wgOut->addWikiMsg( 'deletedtext', wfEscapeWikiText( $deleted ), $loglink );
1489 $wgOut->returnToMain( false );
1490 } else {
1491 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $this->getTitle()->getPrefixedText() ) );
1492 if ( $error == '' ) {
1493 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1494 array( 'cannotdelete', wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) )
1495 );
1496 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1497
1498 LogEventsList::showLogExtract(
1499 $wgOut,
1500 'delete',
1501 $this->getTitle()->getPrefixedText()
1502 );
1503 } else {
1504 $wgOut->addHTML( $error );
1505 }
1506 }
1507 }
1508
1509 /* Caching functions */
1510
1511 /**
1512 * checkLastModified returns true if it has taken care of all
1513 * output to the client that is necessary for this request.
1514 * (that is, it has sent a cached version of the page)
1515 *
1516 * @return boolean true if cached version send, false otherwise
1517 */
1518 protected function tryFileCache() {
1519 static $called = false;
1520
1521 if ( $called ) {
1522 wfDebug( "Article::tryFileCache(): called twice!?\n" );
1523 return false;
1524 }
1525
1526 $called = true;
1527 if ( $this->isFileCacheable() ) {
1528 $cache = HTMLFileCache::newFromTitle( $this->getTitle(), 'view' );
1529 if ( $cache->isCacheGood( $this->mPage->getTouched() ) ) {
1530 wfDebug( "Article::tryFileCache(): about to load file\n" );
1531 $cache->loadFromFileCache( $this->getContext() );
1532 return true;
1533 } else {
1534 wfDebug( "Article::tryFileCache(): starting buffer\n" );
1535 ob_start( array( &$cache, 'saveToFileCache' ) );
1536 }
1537 } else {
1538 wfDebug( "Article::tryFileCache(): not cacheable\n" );
1539 }
1540
1541 return false;
1542 }
1543
1544 /**
1545 * Check if the page can be cached
1546 * @return bool
1547 */
1548 public function isFileCacheable() {
1549 $cacheable = false;
1550
1551 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
1552 $cacheable = $this->mPage->getID()
1553 && !$this->mRedirectedFrom && !$this->getTitle()->isRedirect();
1554 // Extension may have reason to disable file caching on some pages.
1555 if ( $cacheable ) {
1556 $cacheable = wfRunHooks( 'IsFileCacheable', array( &$this ) );
1557 }
1558 }
1559
1560 return $cacheable;
1561 }
1562
1563 /**#@-*/
1564
1565 /**
1566 * Lightweight method to get the parser output for a page, checking the parser cache
1567 * and so on. Doesn't consider most of the stuff that WikiPage::view is forced to
1568 * consider, so it's not appropriate to use there.
1569 *
1570 * @since 1.16 (r52326) for LiquidThreads
1571 *
1572 * @param $oldid mixed integer Revision ID or null
1573 * @param $user User The relevant user
1574 * @return ParserOutput or false if the given revsion ID is not found
1575 */
1576 public function getParserOutput( $oldid = null, User $user = null ) {
1577 global $wgUser;
1578
1579 $user = is_null( $user ) ? $wgUser : $user;
1580 $parserOptions = $this->mPage->makeParserOptions( $user );
1581
1582 return $this->mPage->getParserOutput( $parserOptions, $oldid );
1583 }
1584
1585 /**
1586 * Get parser options suitable for rendering the primary article wikitext
1587 * @return ParserOptions|false
1588 */
1589 public function getParserOptions() {
1590 global $wgUser;
1591 if ( !$this->mParserOptions ) {
1592 $this->mParserOptions = $this->mPage->makeParserOptions( $wgUser );
1593 }
1594 // Clone to allow modifications of the return value without affecting cache
1595 return clone $this->mParserOptions;
1596 }
1597
1598 /**
1599 * Sets the context this Article is executed in
1600 *
1601 * @param $context IContextSource
1602 * @since 1.18
1603 */
1604 public function setContext( $context ) {
1605 $this->mContext = $context;
1606 }
1607
1608 /**
1609 * Gets the context this Article is executed in
1610 *
1611 * @return IContextSource
1612 * @since 1.18
1613 */
1614 public function getContext() {
1615 if ( $this->mContext instanceof IContextSource ) {
1616 return $this->mContext;
1617 } else {
1618 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
1619 return RequestContext::getMain();
1620 }
1621 }
1622
1623 /**
1624 * Info about this page
1625 * @deprecated since 1.19
1626 */
1627 public function info() {
1628 wfDeprecated( __METHOD__, '1.19' );
1629 Action::factory( 'info', $this )->show();
1630 }
1631
1632 /**
1633 * Mark this particular edit/page as patrolled
1634 * @deprecated since 1.18
1635 */
1636 public function markpatrolled() {
1637 wfDeprecated( __METHOD__, '1.18' );
1638 Action::factory( 'markpatrolled', $this )->show();
1639 }
1640
1641 /**
1642 * Handle action=purge
1643 * @deprecated since 1.19
1644 */
1645 public function purge() {
1646 return Action::factory( 'purge', $this )->show();
1647 }
1648
1649 /**
1650 * Handle action=revert
1651 * @deprecated since 1.19
1652 */
1653 public function revert() {
1654 wfDeprecated( __METHOD__, '1.19' );
1655 Action::factory( 'revert', $this )->show();
1656 }
1657
1658 /**
1659 * Handle action=rollback
1660 * @deprecated since 1.19
1661 */
1662 public function rollback() {
1663 wfDeprecated( __METHOD__, '1.19' );
1664 Action::factory( 'rollback', $this )->show();
1665 }
1666
1667 /**
1668 * User-interface handler for the "watch" action.
1669 * Requires Request to pass a token as of 1.18.
1670 * @deprecated since 1.18
1671 */
1672 public function watch() {
1673 wfDeprecated( __METHOD__, '1.18' );
1674 Action::factory( 'watch', $this )->show();
1675 }
1676
1677 /**
1678 * Add this page to $wgUser's watchlist
1679 *
1680 * This is safe to be called multiple times
1681 *
1682 * @return bool true on successful watch operation
1683 * @deprecated since 1.18
1684 */
1685 public function doWatch() {
1686 global $wgUser;
1687 wfDeprecated( __METHOD__, '1.18' );
1688 return WatchAction::doWatch( $this->getTitle(), $wgUser );
1689 }
1690
1691 /**
1692 * User interface handler for the "unwatch" action.
1693 * Requires Request to pass a token as of 1.18.
1694 * @deprecated since 1.18
1695 */
1696 public function unwatch() {
1697 wfDeprecated( __METHOD__, '1.18' );
1698 Action::factory( 'unwatch', $this )->show();
1699 }
1700
1701 /**
1702 * Stop watching a page
1703 * @return bool true on successful unwatch
1704 * @deprecated since 1.18
1705 */
1706 public function doUnwatch() {
1707 global $wgUser;
1708 wfDeprecated( __METHOD__, '1.18' );
1709 return WatchAction::doUnwatch( $this->getTitle(), $wgUser );
1710 }
1711
1712 /**
1713 * Output a redirect back to the article.
1714 * This is typically used after an edit.
1715 *
1716 * @deprecated in 1.18; call $wgOut->redirect() directly
1717 * @param $noRedir Boolean: add redirect=no
1718 * @param $sectionAnchor String: section to redirect to, including "#"
1719 * @param $extraQuery String: extra query params
1720 */
1721 public function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
1722 wfDeprecated( __METHOD__, '1.18' );
1723 global $wgOut;
1724
1725 if ( $noRedir ) {
1726 $query = 'redirect=no';
1727 if ( $extraQuery )
1728 $query .= "&$extraQuery";
1729 } else {
1730 $query = $extraQuery;
1731 }
1732
1733 $wgOut->redirect( $this->getTitle()->getFullURL( $query ) . $sectionAnchor );
1734 }
1735
1736 /**
1737 * Use PHP's magic __get handler to handle accessing of
1738 * raw WikiPage fields for backwards compatibility.
1739 *
1740 * @param $fname String Field name
1741 */
1742 public function __get( $fname ) {
1743 if ( property_exists( $this->mPage, $fname ) ) {
1744 #wfWarn( "Access to raw $fname field " . __CLASS__ );
1745 return $this->mPage->$fname;
1746 }
1747 trigger_error( 'Inaccessible property via __get(): ' . $fname, E_USER_NOTICE );
1748 }
1749
1750 /**
1751 * Use PHP's magic __set handler to handle setting of
1752 * raw WikiPage fields for backwards compatibility.
1753 *
1754 * @param $fname String Field name
1755 * @param $fvalue mixed New value
1756 */
1757 public function __set( $fname, $fvalue ) {
1758 if ( property_exists( $this->mPage, $fname ) ) {
1759 #wfWarn( "Access to raw $fname field of " . __CLASS__ );
1760 $this->mPage->$fname = $fvalue;
1761 // Note: extensions may want to toss on new fields
1762 } elseif ( !in_array( $fname, array( 'mContext', 'mPage' ) ) ) {
1763 $this->mPage->$fname = $fvalue;
1764 } else {
1765 trigger_error( 'Inaccessible property via __set(): ' . $fname, E_USER_NOTICE );
1766 }
1767 }
1768
1769 /**
1770 * Use PHP's magic __call handler to transform instance calls to
1771 * WikiPage functions for backwards compatibility.
1772 *
1773 * @param $fname String Name of called method
1774 * @param $args Array Arguments to the method
1775 */
1776 public function __call( $fname, $args ) {
1777 if ( is_callable( array( $this->mPage, $fname ) ) ) {
1778 #wfWarn( "Call to " . __CLASS__ . "::$fname; please use WikiPage instead" );
1779 return call_user_func_array( array( $this->mPage, $fname ), $args );
1780 }
1781 trigger_error( 'Inaccessible function via __call(): ' . $fname, E_USER_ERROR );
1782 }
1783
1784 // ****** B/C functions to work-around PHP silliness with __call and references ****** //
1785
1786 /**
1787 * @param $limit array
1788 * @param $expiry array
1789 * @param $cascade bool
1790 * @param $reason string
1791 * @param $user User
1792 * @return Status
1793 */
1794 public function doUpdateRestrictions( array $limit, array $expiry, &$cascade, $reason, User $user ) {
1795 return $this->mPage->doUpdateRestrictions( $limit, $expiry, $cascade, $reason, $user );
1796 }
1797
1798 /**
1799 * @param $limit array
1800 * @param $reason string
1801 * @param $cascade int
1802 * @param $expiry array
1803 * @return bool
1804 */
1805 public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) {
1806 return $this->mPage->updateRestrictions( $limit, $reason, $cascade, $expiry );
1807 }
1808
1809 /**
1810 * @param $reason string
1811 * @param $suppress bool
1812 * @param $id int
1813 * @param $commit bool
1814 * @param $error string
1815 * @return bool
1816 */
1817 public function doDeleteArticle( $reason, $suppress = false, $id = 0, $commit = true, &$error = '' ) {
1818 return $this->mPage->doDeleteArticle( $reason, $suppress, $id, $commit, $error );
1819 }
1820
1821 /**
1822 * @param $fromP
1823 * @param $summary
1824 * @param $token
1825 * @param $bot
1826 * @param $resultDetails
1827 * @param $user User
1828 * @return array
1829 */
1830 public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails, User $user = null ) {
1831 global $wgUser;
1832 $user = is_null( $user ) ? $wgUser : $user;
1833 return $this->mPage->doRollback( $fromP, $summary, $token, $bot, $resultDetails, $user );
1834 }
1835
1836 /**
1837 * @param $fromP
1838 * @param $summary
1839 * @param $bot
1840 * @param $resultDetails
1841 * @param $guser User
1842 * @return array
1843 */
1844 public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser = null ) {
1845 global $wgUser;
1846 $guser = is_null( $guser ) ? $wgUser : $guser;
1847 return $this->mPage->commitRollback( $fromP, $summary, $bot, $resultDetails, $guser );
1848 }
1849
1850 /**
1851 * @param $hasHistory bool
1852 * @return mixed
1853 */
1854 public function generateReason( &$hasHistory ) {
1855 return $this->mPage->getAutoDeleteReason( $hasHistory );
1856 }
1857
1858 // ****** B/C functions for static methods ( __callStatic is PHP>=5.3 ) ****** //
1859
1860 /**
1861 * @return array
1862 */
1863 public static function selectFields() {
1864 return WikiPage::selectFields();
1865 }
1866
1867 /**
1868 * @param $title Title
1869 */
1870 public static function onArticleCreate( $title ) {
1871 WikiPage::onArticleCreate( $title );
1872 }
1873
1874 /**
1875 * @param $title Title
1876 */
1877 public static function onArticleDelete( $title ) {
1878 WikiPage::onArticleDelete( $title );
1879 }
1880
1881 /**
1882 * @param $title Title
1883 */
1884 public static function onArticleEdit( $title ) {
1885 WikiPage::onArticleEdit( $title );
1886 }
1887
1888 /**
1889 * @param $oldtext
1890 * @param $newtext
1891 * @param $flags
1892 * @return string
1893 */
1894 public static function getAutosummary( $oldtext, $newtext, $flags ) {
1895 return WikiPage::getAutosummary( $oldtext, $newtext, $flags );
1896 }
1897 // ******
1898 }